package gestores;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
 
public class EmailTextField extends JTextField{
  public EmailTextField(int columns){
    super(columns);
    addVerifier();
  }
 
  private void addVerifier(){
    this.setInputVerifier(new EmailVerifier());
  }
 
  class EmailVerifier extends InputVerifier{    
    public boolean verify(JComponent input) {
      String regx = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
      String text = EmailTextField.this.getText();
 
      if(text.matches(regx)){       
        return true;
      }
      else{
        JOptionPane.showMessageDialog(null, 
            "Invalid email format");
        return false;
      }
    }
  }
}
